/*	CompoundParent.java
 *
 *	Copyright (c) Manateem Challenge Group, Zaia Group, and contributors 2010
 *
 *	This file is part of MSGToolSuite ('this project').
 *
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.bu.manateem.msg.MSGConstants;

/**
 *	<p>
 *	
 *	</p>
 *
 *
 *	@author
 *		Christopher Jacobs
 *
 *	@version
 *		1.0
 *
 *	@changelog
 *		May 10, 2010	- Version 1.0.
 */
public class CompoundParent
{
	/*
	 * TYPES
	 */

	/*
	 * CONSTANTS
	 */

	// Serial versioning
	private static final long serialVersionUID = 1L;
	
	/*
	 * FIELDS
	 */
	private Long id;
	private String name;
	private String formula;
	private String classification;
	private List<Compound> compounds = new ArrayList<Compound>();
	
	/*
	 * CONSTRUCTORS
	 */

	public CompoundParent()
	{
		// TODO: fill in?
	}
	
	public CompoundParent(Long id)
	{
		this.id = id;
	}
	
	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	public CompoundParent(String name)
	{
		this.name = name;
	}
	
	public void addCompound(Compound compound)
	{
		compounds.add(compound);
	}
		
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
		{
			return true;
		}
		if (obj == null)
		{
			return false;
		}
		if (!(obj instanceof CompoundParent))
		{
			return false;
		}
		CompoundParent other = (CompoundParent) obj;
		if (name == null)
		{
			if (other.name != null)
			{
				return false;
			}
		}
		else if (!name.equals(other.name))
		{
			return false;
		}
		return true;
	}
	
	public String getClassification()
	{
		return classification;
	}
	
	public List<Compound> getCompounds()
	{
		return compounds;
	}
	
	public String getFormula()
	{
		return formula;
	}
	
	public Long getID()
	{
		return id;
	}
	
	
	public String getName()
	{
		return name;
	}
	
	public Double getTotalAbundance()
	{
		Double abundance = 0.0;
		for (Compound species : compounds)
		{
			abundance += species.getAbundance();
		}
		return abundance;
	}
	
	public Map<String, IntensityValue> getTotalAbundanceByFile()
	{
		Map<String, IntensityValue> result = new HashMap<String, IntensityValue>();
		for (Compound species : compounds)
		{
			for (IntensityValue value : species.getIntensityValues())
			{
				String fileName = value.getFileName();
				if (result.containsKey(fileName))
				{
					IntensityValue sum = IntensityValue.add(result.get(fileName), value);
					result.put(fileName, sum);
				}
				else
				{
					result.put(fileName, value);
				}
			}
		}
		
		return result;
	}	
	
	public Double getTotalRelativeAbundance()
	{
		Double abundance = 0.0;
		for (Compound species : compounds)
		{
			abundance += species.getRelativeAbundance();
		}
		return abundance;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null)
			? 0
			: name.hashCode());
		return result;
	}
	
	public void setClassification(String classification)
	{
		this.classification = classification;
	}
	
	public void setFormula(String formula)
	{
		this.formula = formula;
	}
	
	public void setID(Long id)
	{
		this.id = id;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("[CompoundParent: " + MSGConstants.LF);
        builder.append(name).append(",");
        builder.append(formula).append(",");
        builder.append(classification).append("]");
		return builder.toString();
	}
}
